package emotionIndex;
import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class InvertedIndexReducer extends Reducer<Text, Text, Text, Text>{
	
	public void reduce(Text key, Iterable<Text> values,
			Context context) throws IOException, InterruptedException {
		
		double posScore[] = {0.0, 0.0, 0.0, 0.0};
		double negScore[] = {0.0, 0.0, 0.0, 0.0};
		int cnt[] = {0, 0, 0, 0};
		
		for (Text val : values) {
			String str[] = val.toString().split("#");
			String posStr = str[0];
			int t = 0;
			double pv = Double.valueOf(str[1]);
			double nv = Double.valueOf(str[2]);
			switch(posStr.charAt(0)){
			case 'n':
				t = 0;
				continue;
				//break;
			case 'a':
				t = 1;
				break;
			case 'v':
				t =2;
				break;
			case 'r':
				t = 3;
				break;
			}
			posScore[t] += pv;
			negScore[t] += nv;
			cnt[t] ++;
		}
		
		String pos[] = {"n", "a", "v", "r"};
		
		for (int i = 0; i < 4; i ++){
			if (cnt[i] != 0){
				String tempStr = pos[i] + "#" + posScore[i] / cnt[i] + "#" + negScore[i] / cnt[i];
				context.write(key, new Text(tempStr));
			}
		}
		
	}
}
